package co.fitstart.entity.courseware.fitnessgroup;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.courseware.movement.Movement;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "courseware")
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Courseware {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 组数 */
    private int groupAmount;
    
    /** 每组动作次数 */
    private int movementNumber;
    
    /** 最大重量 */
    private String maxWeight;
    
    /** 计时 */
    private String time;
    
    /** 备注 */
    private String remark;
    
    /** 动作类别 */
    @Enumerated(EnumType.ORDINAL)
    private CoursewareMovementType type;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @PersistentFormat
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Movement movement;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private DailyCourseware daily;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    public CourseSchedule courseSchedule;
    
    /** 排序 */
    public Integer ranking;
    
    /**
     * The default constructor
     */
    public Courseware() {
    }
    
    /**
     * 
     * @param template
     * @param daily
     */
    public Courseware(CoursewareTemplate template, DailyCourseware daily) {
        this.groupAmount = template.getGroupAmount();
        this.movementNumber = template.getMovementNumber();
        this.maxWeight = template.getMaxWeight();
        this.time = template.getTime();
        this.type = template.getType();
        this.movement = template.getMovement();
        this.daily = daily;
        this.disabled = false;
    }
    
    /**
     * 
     * @param template
     * @param daily
     * @param type
     */
    public Courseware(CoursewareTemplate template, DailyCourseware daily, CoursewareMovementType type) {
        this.groupAmount = template.getGroupAmount();
        this.movementNumber = template.getMovementNumber();
        this.maxWeight = template.getMaxWeight();
        this.time = template.getTime();
        this.type = type;
        this.movement = template.getMovement();
        this.daily = daily;
        this.disabled = false;
    }
    
    /**
     * 
     * @param courseSchedule
     * @param template
     */
    public Courseware(CourseSchedule courseSchedule, CoursewareTemplate template) {
        this.groupAmount = template.getGroupAmount();
        this.movementNumber = template.getMovementNumber();
        this.maxWeight = template.getMaxWeight();
        this.time = template.getTime();
        this.movement = template.getMovement();
        this.courseSchedule = courseSchedule;
        this.disabled = false;
    }
    
    /**
     * 
     * @param courseSchedule
     * @param movement
     */
    public Courseware(CourseSchedule courseSchedule, Movement movement) {
        this.groupAmount = 0;
        this.movementNumber = 0;
        this.movement = movement;
        this.courseSchedule = courseSchedule;
        this.disabled = false;
    }
    
    /**
     * 
     * @param daily
     * @return
     */
    public Courseware initilization(DailyCourseware daily, CoursewareMovementType type) {
        this.daily = daily;
        this.type = type;
        this.disabled = false;
        return this;
    }
    
    /**
     * 
     * @param courseSchedule
     * @return
     */
    public Courseware initilization(CourseSchedule courseSchedule) {
        this.courseSchedule = courseSchedule;
        this.disabled = false;
        return this;
    }
    
    /**
     * 
     * @param groupAmount
     * @param movementNumber
     * @param maxWeight
     * @param time
     * @return
     */
    public Courseware edit(int groupAmount, int movementNumber, String maxWeight, String time) {
        this.groupAmount = groupAmount;
        this.movementNumber = movementNumber;
        this.maxWeight = maxWeight;
        this.time = time;
        
        return this;
    }
    
    /**
     * 
     * @param movement
     * @return
     */
    public Courseware edit(Movement movement) {
        this.movement = movement;
        
        return this;
    }
    
    /**
     * 
     * @param ranking
     * @return
     */
    public Courseware editRanking(int ranking) {
        this.ranking = ranking;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Courseware riseRanking() {
        this.ranking -= 1;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Courseware dropRanking() {
        this.ranking += 1;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Courseware disable() {
        this.disabled = true;
        return this;
    }

    public String getMovementName() {
        return this.movement == null ? null : this.movement.getName();
    }
    
    public String getMovementCover() {
      return this.movement == null ? null : this.movement.getCover();
    }
    
    public String getMovementCategory() {
      return this.movement == null ? null : (this.movement.getCategory() == null ? null : this.movement.getCategory().getName());
    }
    
    public String getMovementVideoLink() {
      return this.movement == null ? null : this.movement.getVideoLink();
    }
    
    public String getMovementEquipment() {
      return this.movement == null ? null : this.movement.getEquipment() == null ? null : this.movement.getEquipment().getName();
    }
    
    public String getMovementEquipmentDesc() {
      return this.movement == null ? null : this.movement.getEquipment() == null ? null : this.movement.getEquipment().getDescription();
    }

    /**
     * 
     * @return
     */
    public String getMovementSummary() {
        return this.movement.getSummary();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the groupAmount
     */
    public int getGroupAmount() {
        return groupAmount;
    }

    /**
     * @return the movementNumber
     */
    public int getMovementNumber() {
        return movementNumber;
    }

    /**
     * @return the maxWeight
     */
    public String getMaxWeight() {
        return maxWeight;
    }

    /**
     * @return the time
     */
    public String getTime() {
        return time;
    }

    /**
     * @return the remark
     */
    public String getRemark() {
        return remark;
    }

    /**
     * @return the type
     */
    public CoursewareMovementType getType() {
        return type;
    }

    /**
     * @return the movement
     */
    public Movement getMovement() {
        return movement;
    }

    /**
     * @return the daily
     */
    public DailyCourseware getDaily() {
        return daily;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @return the courseSchedule
     */
    public CourseSchedule getCourseSchedule() {
        return courseSchedule;
    }

    /**
     * @return the ranking
     */
    public Integer getRanking() {
        return ranking;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param groupAmount the groupAmount to set
     */
    public void setGroupAmount(int groupAmount) {
        this.groupAmount = groupAmount;
    }

    /**
     * @param movementNumber the movementNumber to set
     */
    public void setMovementNumber(int movementNumber) {
        this.movementNumber = movementNumber;
    }

    /**
     * @param maxWeight the maxWeight to set
     */
    public void setMaxWeight(String maxWeight) {
        this.maxWeight = maxWeight;
    }

    /**
     * @param time the time to set
     */
    public void setTime(String time) {
        this.time = time;
    }

    /**
     * @param remark the remark to set
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * @param type the type to set
     */
    public void setType(CoursewareMovementType type) {
        this.type = type;
    }

    /**
     * @param movement the movement to set
     */
    public void setMovement(Movement movement) {
        this.movement = movement;
    }

    /**
     * @param daily the daily to set
     */
    public void setDaily(DailyCourseware daily) {
        this.daily = daily;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @param courseSchedule the courseSchedule to set
     */
    public void setCourseSchedule(CourseSchedule courseSchedule) {
        this.courseSchedule = courseSchedule;
    }

    /**
     * @param ranking the ranking to set
     */
    public void setRanking(Integer ranking) {
        this.ranking = ranking;
    }
}
